#!/usr/bin/python3
# -*- coding: utf-8 -*-

import numpy  as np
import pandas as pd
import talib as ta

eps = 1e-8
# https://bbs.quantclass.cn/thread/18651

def signal(*args):
    df = args[0]
    n = args[1]
    factor_name = args[2]

    df['mtm'] = df['close'] / df['close'].shift(n) - 1

    df['_g']  = 1 - abs((df['close'] - df['open'])/(df['high'] - df['low'] + eps))
    df['gap'] = df['_g'].rolling(window=n, min_periods=1).mean()

    df[factor_name] = df['mtm'].rolling(window=n, min_periods=1).mean()/(df['gap'] + eps)

    return df
